package com.snow.fvmusic.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.snow.fvmusic.bo.CommentAndGreatParam;
import com.snow.fvmusic.dao.MessageDao;
import com.snow.fvmusic.generator.entities.Article;
import com.snow.fvmusic.generator.entities.CommentMessage;
import com.snow.fvmusic.generator.entities.GreatMessage;
import com.snow.fvmusic.generator.mapper.CommentMessageMapper;
import com.snow.fvmusic.generator.mapper.GreatMessageMapper;
import com.snow.fvmusic.service.MessageService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * @Author ：snow
 * @Date ：Created in 2020-10-21
 * @Description：
 * @Modified By：
 * @Version:
 */
@Service
public class MessageServiceImpl implements MessageService {

    @Resource
    private MessageDao messageDao;
    @Resource
    private CommentMessageMapper commentMessageMapper;
    @Resource
    private GreatMessageMapper greatMessageMapper;
    @Override
    public List<CommentAndGreatParam> getGreatMessage(Long userId, Integer limit) {
        List<CommentAndGreatParam> messages = messageDao.getGreatMessage(userId, limit);
        Collections.reverse(messages);
        return messages;
    }

    @Override
    public List<CommentAndGreatParam> getCommentMessage(Long userId, Integer limit) {
        List<CommentAndGreatParam> messages = messageDao.getCommentMessage(userId, limit);
        Collections.reverse(messages);
        return messages;
    }

    @Override
    public void addMessages(CommentAndGreatParam message) {
        if (message.getContent() == null){
            GreatMessage gm = new GreatMessage();
            gm.setArticleId(message.getArticleId());
            gm.setDatetime(LocalDateTime.now());
            gm.setHasRead(false);
            gm.setFromUserId(message.getUserId());
            gm.setUserId(message.getArticleUserId());
            greatMessageMapper.insert(gm);
        }else{
            CommentMessage cm = new CommentMessage();
            cm.setArticleId(message.getArticleId());
            cm.setContent(message.getContent());
            cm.setDatetime(LocalDateTime.now());
            cm.setHasRead(false);
            cm.setUserId(message.getArticleUserId());
            cm.setFromUserId(message.getUserId());
            int insert = commentMessageMapper.insert(cm);
        }
    }
}
